package com.imdroid.carac.speechRecog;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.imdroid.carac.IMDroid;
import com.imdroid.carac.R;

class SpeechRecogDialog {

private static final float totalTime = 1.3f; // seconds
private static final int fps = 20; // frame per second

	
	
	
static View floatView;
private static WindowManager.LayoutParams params;
private static ImageView view;
private static WindowManager windowManager;

static {
	floatView = LayoutInflater.from(IMDroid.getContext()).inflate( R.layout.speech_recog, null );

	view = new ImageView( floatView.getContext() ) {
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			SpeechRecogDialog.onDraw_a(canvas);
		}
	};
	view.setImageResource(R.drawable.speech_recog);

	LinearLayout layout = (LinearLayout) floatView.findViewById( R.id.speech_recog_mic );
	layout.addView(view);
	
	params = new WindowManager.LayoutParams(
			WindowManager.LayoutParams.WRAP_CONTENT,
			WindowManager.LayoutParams.WRAP_CONTENT, 0, 0,
			WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
			WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
					| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
			PixelFormat.TRANSPARENT);
	params.alpha = 1;
//	params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
	windowManager = (WindowManager) IMDroid.getContext()
			.getSystemService(Context.WINDOW_SERVICE);
	windowManager.getDefaultDisplay();
	@SuppressWarnings("deprecation")
	int w = windowManager.getDefaultDisplay().getWidth();
	@SuppressWarnings("deprecation")
	int h = windowManager.getDefaultDisplay().getHeight();
	params.x = -w / 2;
	params.y = h * 5 / 8;
}

static void start() {
	windowManager.addView(floatView, params);
	frame = 0;
	frame();
}

private static int frame;
private static final float max = totalTime * fps;
private static final float anglePerFrame = 360 / max;

private static void frame() {
	if (frame > max) {
		windowManager.removeView( floatView );
		SpeechRecog.onStop();
		return;
	}
	frame++;
	view.invalidate();
	view.postDelayed(new Runnable() {
		public void run() {
			frame();
		}
	}, 1000 / fps);
}

private static Paint paint;
private static void onDraw_a(Canvas canvas) {
	if (frame == 0)
		return;

	int w = view.getWidth();
	int h = view.getHeight();
	int s = Math.min(w, h) / 2;
	int strokeWidth = s / 3;

	if (paint == null) {
		paint = new Paint();
		paint.setStrokeCap(Paint.Cap.ROUND);
		paint.setStyle(Paint.Style.STROKE);
	}
	paint.setStrokeWidth(strokeWidth);

	// paint.setColor(color.white);
	// canvas.drawRect(0, 0, w, h, new Paint());

	paint.setColor(0xD000FF00);
	canvas.drawArc(
			new RectF(w / 2 - s + strokeWidth, h / 2 - s + strokeWidth, w
					/ 2 + s - strokeWidth, h / 2 + s - strokeWidth), -90,
			anglePerFrame * frame, false, paint);
	// canvas.drawCircle(w/2, h/2, 100, paint);
}

/*
 * private static String supportedWords() { String s = ""; try {
 * BufferedReader in = new BufferedReader( new InputStreamReader( new
 * FileInputStream(new File(IMDroid.SPEECH_RECOG_FOLDER,"A.arpa")), "UTF8" )
 * ); while( ! in.readLine().trim().equals("\\data\\") ); while( !
 * in.readLine().trim().equals("\\1-grams:") ); while( true ) { String line
 * = in.readLine().trim(); if( line.equals("") ) break; if(
 * line.contains("s>") ) continue; line = line.substring( line.indexOf( ' '
 * ) + 1 ); if( line.startsWith( "_" ) ) continue; s += line + " "; }
 * in.close(); return s.trim(); } catch( IOException e ) { throw new
 * RuntimeException( e ); } }
 */

}
